package com.airlines.crewassignment.controller.crew;

import com.airlines.base.entity.user.User;
import com.airlines.common.Page;
import com.airlines.common.R;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.service.crew.CrewManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author xxxx
 * @date 2018/7/27
 * @company xxxx Airlines
 * @description 管理干部维护
 */
@Controller
@RequestMapping("/crewManagement")
public class CrewManagementController {
    private static final String PREFIX = "/crew";
    @Autowired
    private CrewManagementService crewManagementService;

    @RequestMapping("toListPage")
    public String toListPage(String dutyCode,Model model) {
        model.addAttribute("dutyCode",dutyCode);
        return PREFIX+"/crewManagementList";
    }
    @RequestMapping("toEditPage")
    public String toEditPage(String workcode,String acType,Model model) {
        Crew crew = crewManagementService.getCrewByWorkcode(workcode);
        model.addAttribute("crew",crew);
        return PREFIX+"/crewManagementEdit";
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    //@RequiresPermissions("crew:crewConflict:list")
    public Page list(Crew crew, String offset, String limit) {
        Page page = new Page();
        page.setPageNumber((Integer.valueOf(offset) / (Integer.valueOf(limit))));
        page.setPageSize(Integer.valueOf(limit));
        page = crewManagementService.getCrewList(crew,page);
        return page;
    }

    /**
     * 保存
     * @param crew
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method={ RequestMethod.POST})
    //@RequiresPermissions("crew:crewConflict:add")
    public R save(HttpServletRequest request,Crew crew){
        User user = (User)request.getSession().getAttribute("currentUser");
        if(crewManagementService.updateManagerByWorkcode(crew)> 0) {
            return R.ok();
        }
        return R.error();
    }
}
